package com.java.comprehensive.arithmetic.leecode.tags.linkedlist;

import util.ListNode;

/**
 * @Author: 窦绍飞
 * @Date: 2022/7/15
 */
public class LeetCode_92 {

	public static ListNode reverseBetween(ListNode head, int left, int right) {

		if (left == right) return head;
		ListNode dummy = new ListNode(0);
		dummy.next = head;

		ListNode pre = dummy;
		ListNode leftNode = head; // pre.next  = mNode
		ListNode rightNode = head;


		for (int i = 1; i < left; i++) {
			pre = leftNode;
			leftNode = leftNode.next;
		}

		for (int i = 1; i < right; i++) {
			rightNode = rightNode.next;
		}

		while (leftNode != rightNode) {
			pre.next = leftNode.next;
			leftNode.next = rightNode.next;
			rightNode.next = leftNode;
			leftNode = pre.next;
		}

		return dummy.next;
	}

	public static void main(String[] args) {
		ListNode listNode3 = new ListNode(5, null);
		ListNode listNode4 = new ListNode(4, listNode3);
		ListNode listNode5 = new ListNode(3, listNode4);
		ListNode listNode6 = new ListNode(2, listNode5);
		ListNode listNode7 = new ListNode(1, listNode6);
		ListNode listNode = reverseBetween(listNode7,2,4);
		System.out.println(listNode);
	}
}
